package pri.zey.custom.activity;/**
 * Created by Administrator on 2016/3/12.
 */

import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * Activity管理类
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public class AManager {

    private Stack<ActivityBase> mActivityStack = new Stack<ActivityBase>();
    private FManager mFManager = new FManager();
    private static AManager aManager;

    private AManager(){

    }

    synchronized public static AManager getInstance(){
        if(aManager == null){
            aManager = new AManager();
        }
        return aManager;
    }

    public int getStackSize(){
        return mActivityStack.size() - 1;
    }

    public Stack<ActivityBase> getmActivityStack(){
        if(mActivityStack != null) {
            return mActivityStack;
        }else {
            return null;
        }
    }

    //添加Activity到stack
    public void addActivity(ActivityBase activity){
        mActivityStack.add(activity);
    }

    //删除stack中的ActivityBase
    public void delActivity(ActivityBase activity){
        for (ActivityBase activityBase : mActivityStack){
            if(activityBase.equals(activity)){
                mActivityStack.remove(activity);
            }
        }
    }

    //插入fragment到stact（把包空position后的元素删除）
    public boolean updateAndDelFragment(int position, ActivityBase activity){
        boolean isState = false;
        int mStackSize = getStackSize();
        if(getStackSize() <= position){
            int index = mStackSize - position;
            //保存包空position位置之后的元素存放到list中，并删除stack中的position位置元素
            while(index-- >= 0){
                mActivityStack.remove(position);
            }
            //把插入fragment插入到stack中
            mActivityStack.add(activity);
            isState = true;
        }else{
            //越界无法插入
            isState = false;
        }
        return isState;
    }

    //插入fragment到stact（把包空position后的元素,存储到stack堆栈后面）
    public boolean updateAndSaveFragment(int position, ActivityBase activity){
        boolean isState = false;
        int mStackSize = getStackSize();
        List<ActivityBase> list = new ArrayList<ActivityBase>();
        if(getStackSize() <= position){
            int index = mStackSize - position;
            //保存包空position位置之后的元素存放到list中，并删除stack中的position位置元素
            while(index-- >= 0){
                list.add(mActivityStack.get(position));
                mActivityStack.remove(position);
            }
            //把插入fragment插入到stack中
            mActivityStack.add(activity);
            //把备份到list中的元素，在存放回来
            for (ActivityBase fragmentBase : list){
                mActivityStack.add(activity);
            }
            isState = true;
        }else{
            //越界无法插入
            isState = false;
        }
        return isState;
    }

    //返回最顶部activity
    public ActivityBase getTopActivity(){
        return mActivityStack.get(getStackSize());
    }

    //返回指定activity
    public ActivityBase getActivity(int position){
        if(getStackSize() <= position){
            return mActivityStack.get(position);
        }else{
            //越界返回空
            return null;
        }
    }

    //finish所有Activity
    public void finishAll(){
        for (ActivityBase activityBase : mActivityStack){
            Log.i("TAGSS", "你finish了：" + activityBase.getmActivityName());
            activityBase.finish();
        }
    }


    /**
     *  ********************************************************************************************
     *  规则：
     *  多个fragment同时显示
     */
    //remove-add
    //使用另一个Fragment替换当前的，实际上就是remove()然后add()的合体~
    public void replace(int layoutId, FragmentBase fragment){
        FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
        //判断当前fragment是否存在
        if(mFManager.isExist(mFManager.getTopFragment())) {
            //移除stack中的当前fragment
            mFManager.delFragment(fragment);
        }
        //添加新的fragment到stack
        if(mFManager.addFragment(fragment)){
            //当fragment不已存在stack
        }else{
            //当fragment已存在stack

        }
        ft.replace(layoutId, fragment);
        commit(getTopActivity().getmActivityName(), ft);
    }

    //从Activity中移除一个Fragment，如果被移除的Fragment没有添加到回退栈（回退栈后面会详细说），这个Fragment实例将会被销毁。
    public void remove(FragmentBase fragment){
        FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
        if(mFManager.isExist(fragment)) {
            mFManager.delFragment(fragment);
            ft.remove(fragment);
        }else{
            //移除一个没有存在的fragment
        }
        commit(getTopActivity().getmActivityName(), ft);
    }

    //从Activity中移除多个Fragment，如果被移除的Fragment没有添加到回退栈，这个Fragment实例将会被销毁。
    public void remove(FragmentBase... fragments){
        FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
        if(fragments != null)
        for (FragmentBase fragmentBase : fragments){
            if(mFManager.isExist(fragmentBase)) {
                mFManager.delFragment(fragmentBase);
                ft.remove(fragmentBase);
            }else{
                //移除一个没有存在的fragment
            }
        }
        commit(getTopActivity().getmActivityName(), ft);
    }

    //往Activity中添加一个Fragment
    public void add(int layoutId,FragmentBase fragment){
        if(mFManager.addFragment(fragment)){
            //如果该Fragment对象被添加到了它的Activity中，那么它返回true，否则返回false。
            if (!fragment.isAdded()) {
                FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
                ft.add(layoutId, fragment, fragment.getmFragmentName());
                commit(getTopActivity().getmActivityName(), ft);
            }else{
                //已存在在activity中
            }
        }else{
            //fragment是否已存在在stack中
        }
    }

    //往Activity中添加多个Fragment
    public void add(int layoutId, FragmentBase... fragments){
        FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
        if(fragments != null)
        for (FragmentBase fragmentBase : fragments){
            if(mFManager.addFragment(fragmentBase)){
                //如果该Fragment对象被添加到了它的Activity中，那么它返回true，否则返回false。
                if (!fragmentBase.isAdded()) {
                    ft.add(layoutId, fragmentBase, fragmentBase.getmFragmentName());
                }else{
                    //已存在在activity中
                }
            }else{
                //fragment是否已存在在stack中
            }
        }
        commit(getTopActivity().getmActivityName(), ft);
    }

    //隐藏多个Fragment，仅仅是设为不可见，并不会销毁
    public void hide(FragmentBase... fragments){
        FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
        for (FragmentBase fragmentBase : fragments){
            //检查fragment是否存在在stack中
            if(!mFManager.isExist(fragmentBase)) {
                //直接隐藏fragment
                if(!fragmentBase.isHidden())
                    ft.hide(fragmentBase);
            }
        }
        commit(getTopActivity().getmActivityName(), ft);
    }

    //隐藏Fragment，仅仅是设为不可见，并不会销毁
    public void hide(FragmentBase fragment){
        FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
        //检查fragment是否存在在stack中
        if(!mFManager.isExist(fragment)) {
            //直接隐藏fragment
            if(!fragment.isHidden())
                ft.hide(fragment);
        }
        commit(getTopActivity().getmActivityName(), ft);
    }

    //隐藏一个不存在stack中的Fragment
    public void hideAdd(FragmentBase fragment){
        FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
        //检查fragment是否存在在stack中
        if(!mFManager.isExist(fragment)) {
            //直接隐藏fragment
            if(!fragment.isHidden())
                ft.hide(fragment);
        }else{
            //先把fragment存储到stack中在隐藏fragment
            mFManager.addFragment(fragment);
            if(!fragment.isHidden())
                ft.hide(fragment);
        }
        commit(getTopActivity().getmActivityName(), ft);
    }

    //显示Fragment，并隐藏当前fragment
    public void show(FragmentBase fragment){
        FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
        //先隐藏执行show操作之前的fragment，在显示新的fragment
        //检查fragment是否存在在stack中
        if (mFManager.isExist(fragment)) {
            //判断fragment是否隐藏
            if(!fragment.isHidden()){
                ft.show(fragment);
            }
        }
        commit(getTopActivity().getmActivityName(), ft);
    }

    //显示Fragment，并隐藏当前fragment
    public void show(FragmentBase... fragments){
        FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
        for (FragmentBase fragmentBase : fragments){
            //先隐藏执行show操作之前的fragment，在显示新的fragment
            //检查fragment是否存在在stack中
            if (mFManager.isExist(fragmentBase)) {
                //判断fragment是否隐藏
                if(fragmentBase.isHidden()){
                    ft.show(fragmentBase);
                }
            }
        }
        commit(getTopActivity().getmActivityName(), ft);
    }

    //显示Fragment，并隐藏当前fragment
    public void showHide(FragmentBase fragment){
        FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
        //先隐藏执行show操作之前的fragment，在显示新的fragment
        //检查fragment是否存在在stack中
        if (mFManager.isExist(fragment)) {
            //判断fragment是否隐藏
            if(fragment.isHidden()){
                ft.show(fragment);
            }
        }else{
            //隐藏当前fragment
            FragmentBase CurtterFragment = mFManager.getTopFragment();
            //判断CurtterFragment是否隐藏
            if(CurtterFragment.isHidden()) {
                //没有隐藏
                ft.hide(CurtterFragment);
            }
            //stack中不存在fragment，往stack中添加fragment
            mFManager.addFragment(fragment);
            ft.show(fragment);
        }
        commit(getTopActivity().getmActivityName(), ft);
    }

    //显示Fragment，并隐藏当前fragment
    public void showHideAll(FragmentBase fragment){
        FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
        //先隐藏执行show操作之前的fragment，在显示新的fragment
        //检查fragment是否存在在stack中
        if (mFManager.isExist(fragment)) {
            //判断fragment是否隐藏
            if(fragment.isHidden()){
                ft.show(fragment);
            }
        }else{

            //隐藏当前fragment
            FragmentBase CurtterFragment = mFManager.getTopFragment();
            //判断CurtterFragment是否隐藏
            if(CurtterFragment.isHidden()) {
                //没有隐藏
                ft.hide(CurtterFragment);
            }
            //stack中不存在fragment，往stack中添加fragment
            mFManager.addFragment(fragment);
            ft.show(fragment);
        }
        commit(getTopActivity().getmActivityName(), ft);
    }

    //将此Fragment从Activity中分离，会销毁其布局，但不会销毁该实例
    public void detach(FragmentBase fragment){
        mFManager.delFragment(fragment);
        FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
        ft.detach(fragment);
        commit(getTopActivity().getmActivityName(), ft);
    }

    //将从Activity中分离的Fragment，重新关联到该Activity，重新创建其视图层次
    public void attach(FragmentBase fragment){
        mFManager.delFragment(fragment);
        FragmentTransaction ft = getTopActivity().getSupportFragmentManager().beginTransaction();
        ft.attach(fragment);
        commit(getTopActivity().getmActivityName(), ft);
    }

    //提交fragment事务
    public void commitBackStack(FragmentTransaction ft){
        ft.addToBackStack(null).commit();
    }

    //提交fragment事务并创建回退栈
    public void commitBackStack(String mBackStackName, FragmentTransaction ft){
        ft.addToBackStack(mBackStackName).commit();
    }

    //提交fragment事务
    public void commit(FragmentTransaction ft){
        ft.commit();
    }

    //提交fragment事务并创建回退栈
    public void commit(String mBackStackName, FragmentTransaction ft){
        ft.commit();
    }

    /**
     *  规则：
     *  多个fragment同时显示
     *  ********************************************************************************************
     */
}
